package com.tien.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.tien.utils.AbstractAction;
import com.tien.utils.CallBackJSONUtil;

/**
 * 
 * @author Tian
 * @Mail tianxin_star@163.com
 * @version date：Mar 8, 2012 2:22:53 PM
 * 
 */
@Controller
@Scope("prototype")
public class CacheManagerAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1666476399032400576L;

	private static final Log logger = LogFactory
			.getLog(CacheManagerAction.class);

	@Autowired
	@Qualifier("ehCache")
	private Cache ehCache;
	private String cacheKey;
	
	private Map<String, Object> results = new HashMap<String, Object>();

	@SuppressWarnings("unchecked")
	public String execute() {
		List<String> list = ehCache.getKeys();
		for (String cacheKey : list) {
			Element element = ehCache.get(cacheKey);
			Map<String, Object> map = (Map<String, Object>) element.getValue();
			results.put(cacheKey, map.get("result"));
		}
		logger.info("");
		return "success";
	}
	
	public String deleteCacheByKey(){
		ehCache.remove(cacheKey);
		CallBackJSONUtil.printSuccessState(null, response);
		return null;
	}

	public Map<String, Object> getResults() {
		return results;
	}

	public void setResults(Map<String, Object> results) {
		this.results = results;
	}

	public String getCacheKey() {
		return cacheKey;
	}

	public void setCacheKey(String cacheKey) {
		this.cacheKey = cacheKey;
	}
}
